package com.kayak.emailWorks.util;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.kayak.dbutil.dao.ComnDao;
import com.kayak.dbutil.sql.SqlResult;
import com.kayak.exception.ErrorException;
import com.kayak.kkweb.sys.SysBeans;

public class EmailTempletMarket {
	//收取邮件的邮箱地址以及发送邮件的邮箱地址
//	public static final String FETCHEMAILADDRESS="kayak-ios1@kayak.com.cn";
	//收取邮件的邮箱密码
//	public static final String FETCHMAILPASSAGES="1qaz@WSX";
	public static final String FETCHEMAILADDRESS="lux@kayak.com.cn";
	//收取邮件的邮箱密码
	public static final String FETCHMAILPASSAGES="Lxaktxl950626";
	//发送邮件title
	public static final String SendMailTitle = "【重要】梦姐姐提醒您提交出差时间啦"; 
	//错误邮件title
	public static final String SubErrTile ="【格式错误】：请您不要回复格式错误提醒邮件【此邮件无需回复】";
	//内容错误title
	public static final String ContErrTile = "【格式错误】：请您按照下面的模板回复哦~【此邮件无需回复】";
	//不同出差时间之间需要换行
	public static final String DiffCitiesShouldTurnLines = "【格式错误提醒】：不同城市出差时间需换行【此邮件无需回复】";
	//出差城市天数为空
	public static final String TravelDaysAreNull ="【格式错误提醒】：您回复的出差天数为空哦~【此邮件无需回复】";
	//出差城市为空
	public static final String TravelCitiesAreNUll = "【格式错误提醒】：请不要回复空的城市名哦~【此邮件无需回复】";
	//请您使用正斜杠/哦~
	public static final String PleaseUseRightLines ="【格式错误提醒】：城市、天数、时间段之间请使用/(正斜杠)隔开~【此邮件无需回复】";
	//回复信息必须包含begin和end两行
	public static final String PleaseContainsBeginAndEnd ="【格式错误提醒】：回复的格式须包括begin和end两行~【此邮件无需回复】";
	
	//判断邮件是否为html格式，以正则的方式解析html标签
	public static String HtmlToText(String content){
		StringBuffer sbf = new StringBuffer();
		Pattern pattern = Pattern.compile(">([^<]+)<");
		Matcher matcher = pattern.matcher(content);
		if (matcher.find()) {
			matcher.reset();
			while (matcher.find()) {
				sbf.append(matcher.group(1)+"\r\n");						
			}
		}
		content = sbf.toString();
		return content;
	}
	
	//删除已经存在的数据
	public static void DeleteAlreadyExist(Map<String, Object> temp){
		ComnDao comnDao = SysBeans.getComnDao();
		try {
			SqlResult sr = comnDao.query("EselectAddress", temp);
			if (sr.next()) {
			comnDao.update("Edelecttraveldaysuminfo", temp);	
			}
		} catch (ErrorException e) {
			e.printStackTrace();
		}
	}
	//更新出差错误信息到日志详情表
	public static void UpdateToLog(Map<String, Object> temp){
		ComnDao comnDao = SysBeans.getComnDao();
		try {
			comnDao.update("EwriteLogToTravelSumLog", temp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	//发送邮件模板
	public static  String SendMailTemplet(String name){
		StringBuffer sb = new StringBuffer();				
		sb.append("亲爱的"+name+"，您好~，又到薪资核算日，梦姐姐提醒您提交出差时间啦~~~<br/>");
		sb.append("出差填写正确打开方式：<br/>");
		sb.append("<br/>");
		sb.append("举个栗子：2017.3.6-2017.3.8在北京出差3天，2017.3.9-2017.3.12在上海出差4天<br/>");
		sb.append("则回复如下红色部分:（包括begin和end两行）<br/>");
		sb.append("<font color=#FF5151><strong> ================begin</strong></font><br/>");
		sb.append("<font color=#FF5151><strong>北京/3/3.6-3.8</strong></font><br/>");
		sb.append("<font color=#FF5151><strong>上海/4/3.9-3.12</strong></font><br/>");
		sb.append("<font color=#FF5151><strong> end==================</strong></font><br/>");
		sb.append("<br/>");
		sb.append("温馨提示：<br/>");
		sb.append("1.按红色部分回复（包括begin和end那两行）<br/>");
		sb.append("2.不同出差城市之间需换行<br/>");
		sb.append("3.本月未出差，请忽略本邮件<br/>");				
		sb.append("4.请您尽快按照格式回复给梦姐姐呐（尽量在30号之前~），拜托拜托~<br/>");
		String content = sb.toString();
		return content;		
	}
	
	//格式错误提醒模板
	public static String ErrorNoticeMailTemplet(String personal){
		StringBuffer sb = new StringBuffer();
		sb.append("亲爱的" + personal + "，您好，您的出差天数回复格式不对哦~<br/>");
		sb.append("<strong>此邮件不需要回复，重新回复请回复主题为【梦姐姐提醒您提交出差时间啦 】的邮件</strong>");
		sb.append("<br/>");
		sb.append("出差填写正确打开方式：<br/>");
		sb.append("回复如下红色部分:<br/>");
		sb.append("<font color=#FF0000><strong> ================begin</strong></font><br/>");
		sb.append("<font color=#FF0000><strong>北京/3/3.6-3.8</strong></font><br/>");
		sb.append("<font color=#FF0000><strong>上海/4/3.9-3.12</strong></font><br/>");
		sb.append("<font color=#FF0000><strong> end==================</strong></font><br/>");
		sb.append("<br/>");
		sb.append("温馨提示：<br/>");
		sb.append("1.回复红色内容（包括begin和end那两行）<br/>");
		sb.append("2.不同出差城市之间需换行<br/>");
		sb.append("3.出差时间只写月份和日期<br/>");
		sb.append("4.此邮件无需回复：重新回复请回复主题【梦姐姐提醒您提交出差时间啦】的邮件<br/>");
		String errorcontent = sb.toString();
		return errorcontent;
	}
	
	//主题错误邮件提醒模板
	public static String SubErrNoticeMailTemplet(String personal){
		StringBuffer sb = new StringBuffer();
		sb.append("亲爱的" + personal + "，您好~<br/>");
		sb.append("如果您的回复存在问题，请不要回复格式错误提醒邮件<br/>");
		sb.append("请重新回复主题为【梦姐姐提醒您提交出差时间啦】的邮件，谢谢您的配合<br/>");
		String SubErrContent = sb.toString();
		return SubErrContent;
	}
	//发送主题错误提醒邮件
	public static void sendErrSubMail(String username,String password,String personal,String from){
		new SendEmailHtml().sendAEmail(username, password,
				EmailTempletMarket.SubErrTile,
				EmailTempletMarket.SubErrNoticeMailTemplet(personal), from);		
	}
	//发送格式错误提醒邮件
	public static void sendErrConMail(String username,String password,String personal,String from){
		new SendEmailHtml().sendAEmail(username, password,
				EmailTempletMarket.ContErrTile,
				EmailTempletMarket.ErrorNoticeMailTemplet(personal), from);		
	}
	//发送格式错误提醒邮件
	public static void sendErrLinesMail(String username,String password,String personal,String from){
		new SendEmailHtml().sendAEmail(username, password,
				EmailTempletMarket.PleaseUseRightLines,
				EmailTempletMarket.ErrorNoticeMailTemplet(personal), from);		
	}
	public static void sendErrStartMail(String username,String password,String personal,String from){
		new SendEmailHtml().sendAEmail(username, password,
				EmailTempletMarket.PleaseContainsBeginAndEnd,
				EmailTempletMarket.ErrorNoticeMailTemplet(personal), from);		
	}

}
